package com.ccy.product.controller;

import com.ccy.common.utils.PageUtils;
import com.ccy.common.utils.R;
import com.ccy.product.entity.AttrAttrgroupRelationEntity;
import com.ccy.product.entity.AttrEntity;
import com.ccy.product.entity.AttrGroupEntity;
import com.ccy.product.service.AttrGroupService;
import com.ccy.product.vo.AttrGroupVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;
import java.util.Map;



/**
 * 属性分组
 *
 * @author ccy
 * @email ccy@gmail.com
 * @date 2023-06-19 21:42:21
 */
@RestController
@RequestMapping("product/attrgroup")
public class AttrGroupController {
    @Autowired
    private AttrGroupService attrGroupService;

    /**
     * 获取分类下所有分组&关联属性
     */
    @GetMapping("/{catelogId}/withattr")
    public R getAllGroupWithAttrByCatelogId(@PathVariable("catelogId") Long catelogId){
        List<AttrGroupVo> attrGroupVos = attrGroupService.getAllGroupWithAttrByCatelogId(catelogId);
        return R.ok().put("data",attrGroupVos);
    }

    /**
     * 删除属性与分组关联关系
     */
    @PostMapping("/attr/relation/delete")
    public R deleteAtrrRelations(@RequestBody List<AttrAttrgroupRelationEntity> attrAttrgroupRelationEntitys){
        attrGroupService.deleteAtrrRelations(attrAttrgroupRelationEntitys);
        return R.ok();
    }

    /**
     * 添加属性与分组关联关系
     */
    @PostMapping("/attr/relation")
    public R addAtrrRelations(@RequestBody List<AttrAttrgroupRelationEntity> attrAttrgroupRelationEntitys){
        attrGroupService.addAtrrRelations(attrAttrgroupRelationEntitys);
        return R.ok();
    }

    /**
     * 获取指定分组关联的所有属性
     */
    @GetMapping("/{attrgroupId}/attr/relation")
    //@RequiresPermissions("product:attrgroup:list")
    public R getAtrrRelations(@PathVariable("attrgroupId") Long attrgroupId){
        List<AttrEntity> data =  attrGroupService.getAtrrRelations(attrgroupId);
        return R.ok().put("data", data);
    }

    /**
     * 获取属性分组没有关联的其他属性
     */
    @GetMapping("/{attrgroupId}/{catelogId}/noattr/relation")
    //@RequiresPermissions("product:attrgroup:list")
    public R getAtrrNotRelations(@RequestParam Map<String, Object> params, @PathVariable("attrgroupId") Long attrgroupId, @PathVariable("catelogId") Long catelogId){
        PageUtils page =  attrGroupService.getAtrrNotRelations(params, attrgroupId, catelogId);
        return R.ok().put("page", page);
    }

    /**
     * 列表
     */
    @RequestMapping("/list/{catelogId}")
    //@RequiresPermissions("product:attrgroup:list")
    public R list(@RequestParam Map<String, Object> params, @PathVariable("catelogId") Long catelogId){
        PageUtils page = attrGroupService.queryPage(params, catelogId);
        return R.ok().put("page", page);
    }

    /**
     * 信息
     */
    @RequestMapping("/info/{attrGroupId}")
    //@RequiresPermissions("product:attrgroup:info")
    public R info(@PathVariable("attrGroupId") Long attrGroupId){
        AttrGroupEntity attrGroup = attrGroupService.getInfo(attrGroupId);
        return R.ok().put("attrGroup", attrGroup);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    //@RequiresPermissions("product:attrgroup:save")
    public R save(@RequestBody AttrGroupEntity attrGroup){
		attrGroupService.save(attrGroup);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    //@RequiresPermissions("product:attrgroup:update")
    public R update(@RequestBody AttrGroupEntity attrGroup){
		attrGroupService.updateById(attrGroup);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    //@RequiresPermissions("product:attrgroup:delete")
    public R delete(@RequestBody Long[] attrGroupIds){
		attrGroupService.removeByIds(Arrays.asList(attrGroupIds));

        return R.ok();
    }

}
